﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FW;
using System.IO;





namespace kserv
{
    public partial class Form1 : Form
    {
        private tconsulta[] tc;
        //dbiDataContext db = new dbiDataContext( ConfigurationManager.ConnectionStrings["dbi_desenvConnectionString"].ToString());

        public Form1()
        {
            InitializeComponent();
            lerLog();
        }


        private void lerLog()
        {
            try
            {
                cIO io = new cIO();
                string caminho = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
                io.NomeArquivo = caminho + "\\kcadlog.txt";
                foreach (string st in io.Tail("30"))
                {
                    tb_res.AppendText(st);
                }
                io = null;
            }
            catch (Exception ex)
            {
                tb_res.Text = "";
                tb_res.Text = ex.Message.ToString();
            }
            // tb_res.Text = io.Tail("5");
        }

        private void configuraçãoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form a = new configuracao(this);
            a.Show();
        }

        private void executarAgoraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //dbinfechk.checkNFE nfe = new checkNFE();
            //nfe.executar(tb_res);
            //nfe = null;
        }

        private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form ab = new AboutBox1();
            ab.Show();
            
        }

       
        private void Form1_Load(object sender, EventArgs e)
        {
       //     carregarConfig();
        }

       
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tb_res.Text = string.Empty;
            lerLog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
        }

        private void instalarCHKNFEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tb_res.Text = string.Empty;
            string caminho = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            caminho = "\"" + caminho + "\\kcsrv.exe\"";
            command cmd = new command();
            tb_res.Text = cmd.ExecuteCommandSync("sc create dbiKcad binpath= " + caminho);
            cmd = null;
        }

        private void desinstalarCHKNFEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tb_res.Text = string.Empty;
            //string caminho = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            //caminho = caminho + "\\chknfe.exe";
            command cmd = new command();
            tb_res.Text = cmd.ExecuteCommandSync("sc delete dbiKcad");
            cmd = null;
        }

        private void startCHNFEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tb_res.Text = string.Empty;
            //string caminho = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            //caminho = caminho + "\\chknfe.exe";
            command cmd = new command();
            tb_res.Text = cmd.ExecuteCommandSync("sc start dbiKcad");
            cmd = null;
        }

        private void stopCHKNFEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tb_res.Text = string.Empty;
            //string caminho = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            //caminho = caminho + "\\chknfe.exe";
            command cmd = new command();
            tb_res.Text = cmd.ExecuteCommandSync("sc stop dbiKcad");
            cmd = null;
        }

        private void queryCHKNFEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tb_res.Text = string.Empty;
            //string caminho = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            //caminho = caminho + "\\chknfe.exe";
            command cmd = new command();
            tb_res.Text = cmd.ExecuteCommandSync("sc query dbiKcad");
            cmd = null;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            controle ctrl = new controle();
            ctrl.rearranjaArquivos();
            ctrl = null;
            //string s = tc[0].threadEmExecucao() ? "0 - acabou \n\r": "0 - rodando \n\r";
            //s = s + ((tc[1].threadEmExecucao()) ? "1 - acabou \n\r" : "0 - rodando \n\r");
            //tb_res.Text = s;
            //arquivos a = new arquivos();
            //a.executar();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            //mpcontrole cont = new mpcontrole();
            //cont.iniciarServicosConsultas();
            //cont.iniciarServicosReConsultas();
            //cont.iniciarFinalizacaoArquivos();


            controle contr = new controle();
            contr.rearranjaArquivos();
            contr.rearranjaArquivosPF();
            contr.iniciarServicosConsultas();
            contr.iniciarServicosReConsultas();
            contr.iniciarFinalizacaoArquivos();


          //  tc = new tconsulta[2];
          //  tc[0] = new tconsulta(8,4, 1);
          ////  tc[1] = new tconsulta(9,4, 2);
          //  tc[0].comecar();
          //  //tc[1].comecar();

            
            //cons.prepararArquivoPJ();
            //cons.consultaPJ();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            mpcontrole cont = new mpcontrole();
            cont.rearranjaArquivos();
            cont.iniciarServicosConsultas();
            cont.iniciarServicosReConsultas();
            cont.iniciarFinalizacaoArquivos();
        }

        private void button5_Click(object sender, EventArgs e)
        {

            //consulta cons = new consulta(Convert.ToInt64(tb_idos.Text), Convert.ToInt32(tb_idemp.Text));

            //cons.nRegistrosPara1Registro();
        }

    }


   

}


